import java.util.*;
import java.util.stream.Collectors;

/**
 * 进制转换
 */
public class Solution {
    // 一个字节8个bit
    // 每两位字符表示一个字节，也就是8。每一行是32个bit。也就是8个字符
    // 从第9个字符开始

    /**
     * 16进制hex、10进制d、8进制o、2进制b
     *
     * @param ip
     */
    public static void ipSolve(String ip) {
        String biaoshi = ip.substring(8, 12);
        String result1 = String.valueOf(Integer.parseInt(biaoshi, 16));

        String biaozhi = ip.substring(12, 13);
        // 16进制转二进制再取前三位转十进制
        String temp1 = String.valueOf(Integer.parseInt(biaozhi, 16));
        String temp2 = Integer.toBinaryString(Integer.parseInt(temp1));
        if (temp2.length() == 1) {
            temp2 = "000" + temp2;
        }
        if (temp2.length() == 2) {
            temp2 = "00" + temp2;
        }
        if (temp2.length() == 3) {
            temp2 = "0" + temp2;
        }
        String temp3 = temp2.substring(0, 3);
        String result2 = String.valueOf(Integer.parseInt(temp3, 2));
        System.out.println(result1 + "," + result2);
    }

    public static void main(String[] args) {
        InvokeInfo s1=new InvokeInfo(1,1);
        InvokeInfo s2=new InvokeInfo(1,1);
        InvokeInfo s3=new InvokeInfo(2,2);
        InvokeInfo s4=new InvokeInfo(3,3);
        InvokeInfo s5=new InvokeInfo(4,3);
        InvokeInfo s6=new InvokeInfo(5,2);
        InvokeInfo s7=new InvokeInfo(5,2);
        InvokeInfo s8=new InvokeInfo(8,4);
        InvokeInfo s9=new InvokeInfo(8,4);
        InvokeInfo s10=new InvokeInfo(8,4);
        int[]result=getInterface(Arrays.asList(s1,s2,s3,s4,s5,s6,s7,s8,s9,s10),1,2);
        System.out.println(result);

    }

    static int[] getInterface(List<InvokeInfo> invokes, int timeSegment, int minLimits){
        Map<Integer,List<InvokeInfo>> map=invokes.stream().collect(Collectors.groupingBy(v->v.interfaceId));
        List<Integer> results=new ArrayList<>();
        for (List<InvokeInfo> item:map.values()){
            item=item.stream().sorted(Comparator.comparing(v->v.time)).collect(Collectors.toList());
            int j=1;
            int left=0;
            int right=item.size();
            while ((left != right - 1)) {
                InvokeInfo temp=item.get(left);
                List<InvokeInfo> tempResults=item.subList(left,right).stream().filter(v->v.time<=temp.time+timeSegment-1&&v.time>= temp.time).collect(Collectors.toList());
                if(tempResults.size()>=minLimits){
                    results.add(temp.interfaceId);
                    break;
                }else {
                    if(j/2==0){
                        right--;
                    }else {
                        left++;
                    }
                }
            }
        }
        return results.stream().sorted(Integer::compareTo).mapToInt(x->x).toArray();
    }


    static class InvokeInfo{
        int time;

        public InvokeInfo(int time, int interfaceId) {
            this.time = time;
            this.interfaceId = interfaceId;
        }

        int interfaceId;
    }
}
